import Foundation
import DCloudUTSFoundation

private func isNonNullObject(_ value: Any?) -> Bool {
	guard let value = value else { return false }
	
	// 排除基础类型和不可合并对象
	return !(value is NSNumber) &&
		!(value is Bool) &&
		!(value is String) &&
		!(value is Date) &&
		!(value is UTSRegExp)
}

private func isSpecial(_ value: Any) -> Bool {
	switch value {
	case is Date, is UTSRegExp, is UTSError:
		return true
	case is NSNumber, is String:
		return true
	default:
		return isReactElement(value)
	}
}

private func isReactElement(_ value: Any) -> Bool {
	// 通过Mirror反射检测属性
	let mirror = Mirror(reflecting: value)
	// 检测类型标识符（适配Swift与React Native的0xeac7标识）
	if let typeField = mirror.descendant("type") as? Int {
		return typeField == 0xeac7
	}
	
	// 备选检测方案：类名匹配
	if String(describing: type(of: value)).contains("RCTView") {
		return true
	}
	
	return false
}

private func isVueElement(_ value: Any) -> Bool {
	return false
}

func IsMergeableObject(_ value: Any?) -> Bool {
	// console.log(value, value is UTSRegExp)
	guard let nonOptionalValue = value else { return false }
	
	return isNonNullObject(nonOptionalValue) && !isSpecial(nonOptionalValue as Any)
}